/**
 * 
 */
package legumes.server.usl.services;

import java.util.Collection;
import java.util.List;

import legumes.client.services.LegumesServices;
import legumes.client.services.ToExcellException;
import legumes.server.bll.BLLLegumesServicesImpl;
import legumes.shared.domain.AdminException;
import legumes.shared.domain.Customer;
import legumes.shared.domain.CustomerDTO;
import legumes.shared.domain.CustomerRole;
import legumes.shared.domain.LatLng;
import legumes.shared.domain.LoginException;
import legumes.shared.domain.MailStallParserException;
import legumes.shared.domain.NewProductMarket;
import legumes.shared.domain.Order;
import legumes.shared.domain.Product;
import legumes.shared.domain.ProductCategory;
import legumes.shared.domain.ProductVariety;
import legumes.shared.domain.SellSession;
import legumes.shared.domain.SellSessionForCustomer;
import legumes.shared.domain.Stall;
import legumes.shared.domain.Store;
import legumes.shared.domain.StoreException;
import legumes.shared.domain.UnicityViolationException;

@SuppressWarnings("serial")
public class LegumesServicesImpl extends AbstractRemoteServiceServlet implements
		LegumesServices {

	static{
		BLLLegumesServicesImpl.getInstance().init();
	}
	
	public LegumesServicesImpl(){
	}

	@Override
	public Customer getCustomer(String name_) {
		return BLLLegumesServicesImpl.getInstance().getCustomer(name_);
	}
	
	@Override
	public Customer getCustomer(long uuid_) {
		return BLLLegumesServicesImpl.getInstance().getCustomer(uuid_);
	}

	@Override
	public SellSessionForCustomer getSellSessionForCustomer() {
		return BLLLegumesServicesImpl.getInstance().getSellSessionForCustomer(getMyCustomerDTO());
	}

	@Override
	public SellSession getSellSession(long date_) {
		return BLLLegumesServicesImpl.getInstance().getSellSession(date_);
	}

	@Override
	public void setStoreOpened(boolean b_) {
		BLLLegumesServicesImpl.getInstance().setStoreOpened(b_);
	}

	@Override
	public boolean isStoreOpened() {
		return BLLLegumesServicesImpl.getInstance().isStoreOpened();
	}

	@Override
	public CustomerDTO whoAmI() {
		return getMyCustomerDTO();
	}

	@Override
	public String log(String login_, String password_)
			throws LoginException {
		if (logMe(login_, password_)) {
			return getThreadLocalRequest().getSession().getId();// getMyCustomerDTO();
		}
		throw new LoginException(
				"Utilisateur inconnu ou mot de passe incorrect");
	}

	@Override
	public void unlog() {
		unlogMe();
	}

	@Override
	public long getOpenedSellSessionId() {
		return BLLLegumesServicesImpl.getInstance().getOpenedSellSessionId();
	}

	@Override
	public List<Long> getSellSessionsDate() {
		return BLLLegumesServicesImpl.getInstance().getSellSessionsDate();
	}

	@Override
	public List<Product> getProducts() {
		return BLLLegumesServicesImpl.getInstance().getProducts();
	}

	@Override
	public boolean hasOrderConflict(long sellSessionId_, Order order_) {
		return BLLLegumesServicesImpl.getInstance().hasOrderConflict(getMyCustomerDTO(), sellSessionId_, order_);
	}
	
	@Override
	public Order save(long sellSessionId_, Order order_, boolean merge_)
			throws LoginException, StoreException {
		return BLLLegumesServicesImpl.getInstance().save(getMyCustomerDTO(), sellSessionId_, order_, merge_);
	}

	@Override
	public Collection<Order> getOrders(long sellSessionId_) {
		return BLLLegumesServicesImpl.getInstance().getOrders(sellSessionId_);
	}

	@Override
	public void save(Stall stall_) {
		BLLLegumesServicesImpl.getInstance().save(stall_);
	}

	@Override
	public SellSession newSellSession(Store store_, long fromSellSessionId_) {
		return BLLLegumesServicesImpl.getInstance().newSellSession(store_, fromSellSessionId_);
	}

	@Override
	public List<CustomerDTO> getCustomers() {
		return BLLLegumesServicesImpl.getInstance().getCustomers();
	}
	
	@Override
	public Customer addCustomer(Customer c_) throws UnicityViolationException {
		return BLLLegumesServicesImpl.getInstance().addCustomer(c_);
	}

	@Override
	public void removeCustomer(String customerId_) throws AdminException, LoginException {
		BLLLegumesServicesImpl.getInstance().removeCustomer(getMyCustomerDTO(), customerId_);
	}
	

	@Override
	public void changePassword(String password_, String newPassword_)
			throws LoginException {
		BLLLegumesServicesImpl.getInstance().changePassword(getMyCustomerDTO(), password_, newPassword_);
	}
	
	@Override
	public void setCustomerRole(String customerId_, CustomerRole role_) {
		BLLLegumesServicesImpl.getInstance().setCustomerRole(customerId_, role_);
	}

	@Override
	public void setMessageStore(String message_) {
		BLLLegumesServicesImpl.getInstance().setMessageStore(message_);
	}

	@Override
	public String getMessageStore() {
		return BLLLegumesServicesImpl.getInstance().getMessageStore();
	}

	@Override
	public Stall parseStallMail(String mailContent_)
			throws MailStallParserException {
		MailStallParser mailStallParser = new MailStallParser(mailContent_);
		mailStallParser.parse();
		Stall stall = mailStallParser.getStall();
		return stall;
	}
	
	@Override
	public String generateOrdersReport(long sellSessionId_)
			throws ToExcellException {
		return BLLLegumesServicesImpl.getInstance().generateOrdersReport(sellSessionId_);
	}

	@Override
	public Product[] getProductsStartingWith(String prefix_) {
		return BLLLegumesServicesImpl.getInstance().getProductsStartingWith(prefix_);
	}
	

	@Override
	public boolean validateNewProductMarket(NewProductMarket newProductMarket_) {
		return BLLLegumesServicesImpl.getInstance().validateNewProductMarket(newProductMarket_);
	}

	@Override
	public Product addNewProductMarket(NewProductMarket newProductMarket_) {
		return BLLLegumesServicesImpl.getInstance().addNewProductMarket(newProductMarket_);
	}

	@Override
	public ProductVariety[] getProductVarities() {
		return BLLLegumesServicesImpl.getInstance().getProductVarities();
	}

	@Override
	public ProductCategory[] getProductCategories() {
		return BLLLegumesServicesImpl.getInstance().getProductCategories();
	}

	@Override
	public String resetPassword(String email_) {
		return BLLLegumesServicesImpl.getInstance().resetPassword(email_);
	}

	@Override
	public List<Store> getUserStores(String userId_) {
		return BLLLegumesServicesImpl.getInstance().getUserStores(userId_);
	}

	@Override
	public boolean removeStore(Store store_) {
		return BLLLegumesServicesImpl.getInstance().removeStore(store_);
	}

	@Override
	public boolean isStoreNameExists(String storeName) {
		return BLLLegumesServicesImpl.getInstance().isStoreNameExists(storeName);
	}

	@Override
	public Store addStore(Store storeToAdd) {
		return BLLLegumesServicesImpl.getInstance().addStore(storeToAdd);
	}

	@Override
	public List<Store> getStores(LatLng northEastBound_, LatLng southWestBound_) {
		return BLLLegumesServicesImpl.getInstance().getStores(northEastBound_, southWestBound_);
	}

	@Override
	public void save(Store store_) {
		BLLLegumesServicesImpl.getInstance().save(store_);
	}

}
